<script lang="ts" setup>
import { addDeviceCollection } from '@/api/room';
import type { deviceCollection } from '@/props/room';
import { useRoomStore } from '@/stores/room';
import { ElMessage } from 'element-plus';
import {  reactive, } from 'vue';

const roomForm = reactive<deviceCollection>({
  deviceCollectionName:'',
  roomId: null,
})

//获取房间
const roomStore = useRoomStore()
const roomOptions = roomStore.roomData


const isroomFormEmpty = (roomForm:deviceCollection) => {
  if(roomForm.roomId === null || roomForm.deviceCollectionName ==='') return true
  return false
}

//
const addDeviceCollectionData = async() => {
  if(isroomFormEmpty(roomForm)) return ElMessage.error("设备集合名|房间不能为空")
  const {data} = await addDeviceCollection(roomForm.roomId,roomForm.deviceCollectionName,)
  if(data.code===1) {
    return ElMessage.success("设备集合添加成功")
  }
  return ElMessage.error("设备集合添加成功")
}

const formLabelWidth = '150px'
</script>

<template>
  <div>
    <el-dialog v-model="roomStore.showOnCollectionsPage" title="添加设备集合" width="500">
      <el-form :model="roomForm">
        <el-form-item label="设备集合名" :label-width="formLabelWidth">
          <el-input v-model="roomForm.deviceCollectionName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="房间" :label-width="formLabelWidth">
          <el-select v-model="roomForm.roomId" placeholder="请选择房间">
              <el-option v-for="room in roomOptions" :key="room.roomId" :label="room.roomName" :value="room.roomId"/>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="roomStore.showOnCollectionsPage=false">取消</el-button>
          <el-button type="primary" @click="addDeviceCollectionData">
            提交
          </el-button>
        </div>
      </template>
  </el-dialog>
</div>
</template>

<style lang="scss" scoped>
:deep(.el-dialog) {
  background-color: rgb(47, 59, 82);
}

:deep(.el-dialog__title){
    color:white;
    margin-left: 50px;
  }
</style>
